<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="870"
    @close="close"
  >
    <el-form
      ref="formRef"
      inline
      label-width="140px"
      :model="form"
      :rules="rules"
    >
      <el-form-item label="上级分类：" prop="parentId">
        <el-tree-select
          v-model="form.parentId"
          :check-strictly="true"
          :data="tree"
          node-key="id"
          :props="defaultProps"
          :render-after-expand="false"
          show-checkbox
        />
      </el-form-item>
      <el-form-item label="分类名称：" prop="fullName">
        <el-input v-model="form.fullName" />
      </el-form-item>
      <el-form-item label="分类编码：" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <!-- <el-form-item label="分类：" prop="category">
        <el-select v-model="form.category">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="显示顺序：" prop="sortCode">
        <el-input v-model="form.sortCode" />
      </el-form-item>
      <el-form-item label="是否有效：" prop="Enabled">
        <el-switch v-model="form.order" active-value="1" inactive-value="0" />
      </el-form-item>
      <el-form-item label="备注：">
        <el-input
          v-model="form.remark"
          autosize
          placeholder="请输入备注"
          type="textarea"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  // import VabIconSelector from '@/plugins/VabIconSelector'
  import { doEdit, getDataDictionary } from '@/api/dictionary'

  export default defineComponent({
    name: 'DictionaryTreeEdit',
    // components: { VabIconSelector },
    props: {
      tree: {
        type: Array,
        default: () => [],
      },
    },
    emits: ['tree-save'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        params: {},
        form: {
          meta: {},
        },
        queryForm: {
          dataDictionaryId: 'string',
          dataDictionaryDetailId: 'string',
          parentId: 'string',
          code: 'string',
        },
        defaultProps: {
          children: 'ChildNodes',
          label: 'text',
        },
        rules: {
          parentId: [
            { required: true, trigger: 'change', message: '请选择上级分类' },
          ],
          code: [
            { required: true, trigger: 'blur', message: '请输入分类编码' },
          ],
          fullName: [
            { required: true, trigger: 'blur', message: '请输入分类名称' },
          ],
          remark: [{ required: true, trigger: 'blur', message: '请输入备注' }],
        },
        title: '',
        dialogFormVisible: false,
        type: '',
      })
      //获取详情数据
      const getdataDictionary = async (row) => {
        state.listLoading = true
        const {
          data: { data },
        } = await getDataDictionary(row)
        state.form = data
        state.listLoading = false
      }
      //编辑传值
      const showEdit = (row, type) => {
        state.type = type
        if (type != 'edit') {
          state.title = '添加'
          state.form.parentId = row.id
          state.form.companyId = row.companyId
        } else {
          state.title = '编辑'
          state.queryForm.dataDictionaryId = row.id
          getdataDictionary(state.queryForm)
        }
        state.dialogFormVisible = true
      }
      //弹框关闭
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          meta: {
            icon: '',
          },
        }
        state.dialogFormVisible = false
      }
      //数据保存
      const save = async () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { message } = await doEdit(state.form)
            $baseMessage(message, 'success', 'vab-hey-message-success')
            emit('tree-save')
            close()
          }
        })
      }

      return {
        ...toRefs(state),
        showEdit,
        close,
        getdataDictionary,
        save,
      }
    },
  })
</script>
<style scoped>
  .el-form {
    display: flex;
    flex-flow: wrap;
    justify-content: left;
  }
  .el-form >>> .el-input {
    width: 234px !important;
  }
  .el-form-item {
    width: 380px;
  }
</style>
